package miniDVD;

import java.util.Scanner;

public class MiniDVD {
	public static void main(String[] args) {
		// 标识是否退出，如果flag==false代表不退出，如果false==true代表退出
		boolean flag = false;
		int num1 = 0;
		// 创建接收输入流
		Scanner input = new Scanner(System.in);
		// DVD状态，0代表可借，1代表已借出
		int[] states = new int[6];
		// DVD名称
		String[] names = new String[6];
		// DVD借出日期
		int[] dates = new int[6];
		// DVD借出次数
		int[] counts = new int[6];
		
		// 初始化数据
		states[0] = 0;
		names[0] = "java";
		dates[0] = 0;
		counts[0] = 12;
		
		states[1] = 0;
		names[1] = "三国演义";
		dates[1] = 0;
		counts[1] = 21;
		
		states[2] = 1;
		names[2] = "邓肯自传";
		dates[2] = 17;
		counts[2] = 55;
		
		do{
			System.out.println("欢迎使用迷你DVD管理器");
			System.out.println("------------------------");
			System.out.println("1.新增DVD");
			System.out.println("2.查看DVD");
			System.out.println("3.删除DVD");
			System.out.println("4.借出DVD");
			System.out.println("5.归还DVD");
			System.out.println("6.退出DVD");
			System.out.println("------------------------");
			System.out.print("请选择：");
			int choice = input.nextInt();
			switch(choice){
			case 1:
				// 新增DVD
				System.out.println("---> 新增DVD\n");
				System.out.print("请输入DVD名称：");
				String name = input.next();
				// 标识能否新增，false:不能新增,true:能新增
				boolean flag1 = false;
				for (int i = 0; i < names.length; i++){
					if (names[i] == null){
						flag1 = true;
						names[i] = name;
						break;
					}
				}
				if (flag1){
					System.out.println("新增DVD《"+name+"》成功");
				}else{
					System.out.println("增加失败，货架已满");
				}
				break;	
			case 2:
				// 查看DVD
				System.out.println("--->查看DVD");
				System.out.println("序号\t\t状态\t\t名称\t\t借出日期\t\t借出次数");
				for (int i = 0; i < names.length; i++){
					// 循环遍历数组，输出数组的非空元素信息
					if (names[i]!=null){
						String state = (states[i]==0)?"可借":"已借出";
						String date = (dates[i]==0)?"":dates[i]+"日";
						String count = counts[i]+"次";
						System.out.println(i+1+"\t\t"+state+"\t\t《"+names[i]+"》\t\t"+date+"\t\t"+count);
					}
				}
				break;
			case 3:
				// 删除DVD
				System.out.println("--->删除DVD\n");
				System.out.print("请输入DVD名称：");
				String delete = input.next();
				// 标识能否找到要删除的DVD,true:能根据用户输入的DVD名称找到匹配的DVD，false:不能找到匹配的DVD，不能删除!
				boolean flag2 = false;
				int index = -1;
				for (int i = 0; i < names.length; i++) {
					if (delete.equals(names[i])&&states[i]==1){
						//能找到，但是被借出去了
						flag2 = true;
						System.out.println("对不起要删除的DVD已被借出，不能删除");
						break;
					}else if(delete.equals(names[i])&&states[i]==0){
						// 能找到，可以删除
						flag2 = true;
						index = i;
						System.out.println("删除DVD"+names[i]+"成功!");
						break;
					}
				}
				//找到了DVD才删除
				if (index!=-1){
					// 根据上面找到的下标位置index，删除DVD
					for (int i = index; i < names.length;i++){
						//后面的值一次往前覆盖，最后一个值变为null
						if (i!=names.length-1){
							names[i] = names[i+1];
							states[i] = states[i+1];
							dates[i] = dates[i+1];
							counts[i] = counts[i+1];
						}
					}
					names[names.length-1] = null;
					states[names.length-1] = 0;
					dates[names.length-1] = 0;
					counts[names.length-1] = 0;
				}
				if (!flag2){
					System.out.println("对不起，没有找到匹配的DVD信息，不能删除！");
				}
				break;
			case 4:
				// 借出DVD
				System.out.println("--->借出DVD");
				System.out.print("请输入DVD名称：");
				String lend = input.next();
				for (int i = 0; i < names.length; i++){
					// 能够找到匹配的DVD
					if(lend.equals(names[i])){
						// 但是已经被借出
						if (states[i]==1){
							System.out.println(names[i]+"已被借出");
							break;
						}else if (states[i]==0){
							// 没有被借出，可以借
							System.out.print("请输入借出日期：");
							dates[i] = input.nextInt();
							while(dates[i]<1||dates[i]>31){
								System.out.print("必须输入大于等于1且小于等于31的数字，请重新输入：");
								dates[i] = input.nextInt();
							}
							states[i] = 1;
							counts[i]++;
							System.out.println("借出《"+lend+"》成功");
							break;
						}
					// 找不到匹配的DVD
					}else if(names[i]==null){	
						System.out.println("没有找到匹配的DVD");
						break;
					}
				}
				break;
			case 5:
				// 归还DVD
				System.out.println("--->归还DVD");
				System.out.print("请输入DVD名称：");
				String back = input.next();
				for (int i = 0; i < names.length; i++){
					// 能够找到匹配的DVD
					if(back.equals(names[i])){
						// 但是没有被借出，不用归还
						if (states[i]==0){
							System.out.println(names[i]+"没有被借出，不用归还");
							break;
						}else if (states[i]==1){
							// 被借出，可以归还
							System.out.print("请输入归还日期：");
							// 记录归还日期
							int backDate = input.nextInt();
							while(backDate<dates[i]||backDate>31){
								if(backDate<dates[i]){
									System.out.print("归还日期不能小于借出日期，请重新输入：");
									backDate = input.nextInt();
								}else{
									System.out.print("一个月只有31天，请重新输入");
									backDate = input.nextInt();
								}
							}
							//计算租金，1天1元
							int money = (backDate-dates[i])*1;
							System.out.println("\n归还《"+back+"》成功");
							System.out.println("借出日期为："+dates[i]+"日");
							System.out.println("归还日期为："+backDate+"日");
							System.out.println("应付租金(元)："+money+"元");
							// 数据更新
							states[i] = 0;
							dates[i] = 0;
							break;
						}
					// 找不到匹配的DVD
					}else if(names[i]==null){
						System.out.println("没有找到匹配的DVD");
						break;
					}
				}
				break;
			case 6:
				// 退出DVD
				flag = true;
				break;
				
			default:
				flag = true;
				break;	
			}
			if(flag){
				//想要退出菜单选择
				num1 = -1;
				break;
			}else{
				//不退出，继续进入主菜单进行选择
				System.out.println("**********************");
				System.out.println("输入0返回");
				num1 = input.nextInt();
			}
		}while(num1==0);
		// 程序结束，退出
		System.out.println("谢谢使用");
	}
}
